<template>
    
    <!-- 组件的模板 -->
    <div class="page user-center">
        <div class="weui-cells">
            <a class="weui-cell weui-cell_access mod-user" href="javascript:;" @click="gotoMyInfo">
                <div class="weui-cell__hd userNameLength">
                    <p style="font-size: 26px; font-weight: bolder; height: 31px; line-height: 31px;">{{userName}}</p>
                </div>
                <div class="weui-cell__bd" >
                    <i class="certification" v-if="real_name_auth"></i>
                </div>
                <div class="weui-cell__hd">
                    <!-- <img @click.stop="bigPicture" style="width: 50px; height: 50px; border-radius: 50px;vertical-align: middle;" class="avatar" :src="imgurl" alt="" v-if="imgurl"> -->
                    <img @click.stop="bigPicture" style="width: 50px; height: 50px; border-radius: 50px;vertical-align: middle; margin-right: 5px;" :src="imgurl" alt="" v-if="imgurl">
                    <img style="width: 50px; height: 50px; border-radius: 50px;vertical-align: middle; margin-right: 5px;" v-else src="../../static/imgs/default_avatar.png" alt="">
                </div>
                <div class="weui-cell__ft"></div>
            </a>            
        </div>
        <div class="weui-cells">
            <a class="weui-cell weui-cell_access" href="javascript:;" @click="mywallet">
                <div class="weui-cell__hd"><span class="icon icon_7"></span></div>
                <div class="weui-cell__bd">
                    <p>我的钱包</p>
                </div>
                <div class="weui-cell__ft"><span class="balance">{{balance}}元</span></div>
            </a>
            <a class="weui-cell weui-cell_access" href="javascript:;" @click="linkstr">
                <div class="weui-cell__hd"><span class="icon icon_2"></span></div>
                <div class="weui-cell__bd">
                    <p>使用指南</p>
                </div>
                <div class="weui-cell__ft"></div>
            </a>
            <a class="weui-cell weui-cell_access" href="javascript:;" @click="callme">
                <div class="weui-cell__hd"><span class="icon icon_3"></span></div>
                <div class="weui-cell__bd">
                    <p>联系客服</p>
                </div>
                <div class="weui-cell__ft" style="padding-right: 18px;">{{phone}}</div>
            </a>
            <a class="weui-cell weui-cell_access" href="javascript:;" @click="recommend">
                <div class="weui-cell__hd"><span class="icon icon_4"></span></div>
                <div class="weui-cell__bd">
                    <p>推荐给好友</p>
                </div>
                <div class="weui-cell__ft"></div>
            </a>             
        </div>

        <div class="weui-cells" v-if="has_protocol">
        <!-- <div class="weui-cells"> -->
            <a class="weui-cell weui-cell_access" href="dmsaas://MyAgreement">
                <div class="weui-cell__hd"><span class="icon icon_5 bg_agreement"></span></div>
                <div class="weui-cell__bd">
                    <p>我的合同</p>
                </div>
                <div class="weui-cell__ft"></div>
            </a>
        </div>
    </div> 
</template>

<script>
    /**
     *  引入工具类库 或 Native API
     *  example : import widget from 'gapi/widget'
     */
    import clientUi from 'gapi/clientUi.js'
    import widget from 'gapi/widget.js'
    import cache from 'gapi/cache.js'
    import * as util from 'js/util.js'
    import teamApi from 'clientApi/teamApi.js'
    import socialApi from 'clientApi/socialApi.js'
    import commonPage from 'clientApi/commonPage.js'
    import KCApiApp from 'clientApi/KCApiApp.js'
    /**
     *  引入组件 以及 组件接口(Action)
     *  example : import Somecomponents from 'components/Somecomponents/Somecomponents'
     */

    /**
     *  组件实例对象
     */
    export default {
        components: {

        },
        data: function(){
            return{
                isshow:false,
                isteamname:false,
                dmUserId:'',
                imgurl:'',
                imgurl_big:'',
                userName:'',
                teamname:'',
                teamid:'',
                phone:'010-57977077',
                has_protocol:false,
                real_name_auth:false,
                balance: '',
            }
        },
        computed:{
            // linkstr(){     
            //     return `dmsaas://dmbrowser/${encodeURIComponent('https://doumi.kf5.com/hc/')}`
            // }
        },
        vuex: {
            getters: {

            }, 
            actions: {

            }
        },
        methods: {
            /**
             * 初始化
             * @method init
             * @public
             * @return {Null} void
             */
            init (){
                let dmUserId = util.getQueryString('dmUserId')
                this.dmUserId = dmUserId            
                this.myInitInfoAjax()  // 初始化个人姓名及头像
                this.getBanlance()
                window.loadData = (data) => {
                    this.getBanlance()
                    //console.log(data)
                    if( data && data.key == 'selectTeam'){
                        this.setDefaultTeam(data.value.team_id,data.value.team_name)
                    }else if(data && data.key == 'kong'){
                        util.ajax({
                            url:'/team/list/simple',
                            type:'GET',
                            data:{
                                team_id:0,
                                project_id:0
                            },
                            timeout:10000,
                            success:(obj) => {
                                console.log(obj)
                                if(obj&&obj.errno == 0){
                                    if(obj.data.length == 1){
                                        this.setDefaultTeam(obj.data[0].team_id,obj.data[0].team_name)
                                    }else{
                                        this.myInitInfoAjax()
                                    }
                                }
                                
                            },
                            error: (xhr, status) => {
                                widget.toast('服务器异常')
                            },
                            noNetwork: () => {
                                widget.toast('网络连接失败，请检查网络')
                            }
                        })
                        return false
                    }else{
                        this.myInitInfoAjax()
                    }
                }
                commonPage.getWebViewID((data)=>{
                    cache.setMemoryCache('myIndexwebViewID',{'webViewID':data})
                })
            },
            // 设置默认企业
            setDefaultTeam(team_id,team_name){
                util.ajax({
                    url:'/user/default_team/set',
                    type:'POST',
                    data:{
                        team_id:team_id,
                        project_id:0
                    },
                    timeout:10000,
                    success:(obj) => {
                        console.log(obj)
                        if(obj&&obj.errno == 0){
                            this.teamid = team_id
                            this.teamname = team_name
                            teamApi.updateTeam(team_id);   
                        }else{
                            widget.toast(obj.errmsg)
                        }
                    },
                    error: (xhr, status) => {
                        widget.toast('服务器异常')
                    },
                    noNetwork: () => {
                        widget.toast('网络连接失败，请检查网络')
                    }
                })
            },
            /*初始化个人头像及姓名*/
            myInitInfoAjax (){
                util.ajax({
                    url:'/user/info',
                    type:'GET',
                    data:{
                        team_id:0,
                        project_id:0
                    },
                    timeout:10000,
                    success:(obj) => {
                        console.log(JSON.stringify(obj))
                        if(obj&&obj.errno == 0){
                            this.isshow = true
                            this.imgurl = obj.data.logo_thumb_url;
                            this.imgurl_big = obj.data.logo_url;
                            this.userName = obj.data.real_name;
                            this.user_id = obj.data.user_id;
                            this.has_protocol = obj.data.has_protocol;
                            this.real_name_auth = obj.data.real_name_auth;
                            cache.setCache('user_id', obj.data.user_id, 60*60*24)
                            if(obj.data.team_id && obj.data.team_name != ''){
                                this.isteamname = true
                                this.teamname = obj.data.team_name
                                this.teamid = obj.data.team_id
                            }else{
                                this.isteamname = false
                            }
                        }else{
                            widget.toast(obj.errmsg)
                        }
                    },
                    error: (xhr, status) => {
                        widget.toast('服务器异常')
                    },
                    noNetwork: () => {
                        widget.toast('网络连接失败，请检查网络')
                    }
                })
            },
            //当没有企业时，去创建企业
            createTeam (){
                let url = `backWhereParam=${222}&dmUserId=${this.dmUserId}&user_id=${this.user_id}`
                location.href = "dmsaas://CreateTeam?urdData="+encodeURIComponent(url)
            },
            /**
             * 头像大图预览
             */
            bigPicture (){
                let that = this;
                let title = '个人头像';
                let imgArr = [];
                let subUrl = that.imgurl_big;
                // imgArr.push(util.baseImageDomain+subUrl.substring(1)); 
                imgArr.push(subUrl); 
                let index = imgArr.length-1;
                clientUi.bigPicturePreview(title, imgArr, index)
            },
            /*跳转到个人信息*/
            gotoMyInfo (){
                //埋点
                let statisticsLog_data = {
                    event_id: '我-个人信息',
                    // event_label: data.event_label,(预留，现在不用传此参数)
                    event_param: {
                        ca_from: 'personal',
                    }
                }
                // alert(JSON.stringify(statisticsLog_data))
                KCApiApp.statisticsLog(statisticsLog_data)

                location.href = `dmsaas://PersonInfo`
            },
            /*跳转到团队列表*/
            gotoTeamList (teamid){ 
                let encodeStr = encodeURIComponent(`dmUserId=${this.dmUserId}&team_id=${teamid}&backWhereParam=${222}&user_id=${this.user_id}`)
                location.href = `dmsaas://TeamList?urdData=${encodeStr}`
            },
            // 前往企业详情页
            gotoTeamDetail(team_id,teamname){
                //埋点
                let statisticsLog_data = {
                    event_id: '我-企业',
                    // event_label: data.event_label,(预留，现在不用传此参数)
                    event_param: {
                        ca_from: 'company',
                    }
                }
                // alert(JSON.stringify(statisticsLog_data))
                KCApiApp.statisticsLog(statisticsLog_data)
                let encodeStr = encodeURIComponent(`teamId=${team_id}&dmUserId=${this.dmUserId}&backWhereParam=${222}&team_name=${teamname}&user_id=${this.user_id}`)
                location.href = `dmsaas://MemberManage?urdData=${encodeStr}`
            },
            /*联系客服*/
            callme (){
                //埋点
                let statisticsLog_data = {
                    event_id: '我-联系客服',
                    // event_label: data.event_label,(预留，现在不用传此参数)
                    event_param: {
                        ca_from: 'lxkf',
                    }
                }
                // alert(JSON.stringify(statisticsLog_data))
                KCApiApp.statisticsLog(statisticsLog_data)

                let title = '拨打电话';
                let message = this.phone;
                let okBtn = '确定';
                let cancelBtn = '取消';
                let imageName = '';
                clientUi.showPhoneDialog(title, message, okBtn, cancelBtn, imageName, () => {})
            },
            /*推荐好友--分享*/
            recommend (){
                //埋点
                let statisticsLog_data = {
                    event_id: '我-推荐给好友',
                    // event_label: data.event_label,(预留，现在不用传此参数)
                    event_param: {
                        ca_from: 'share',
                    }
                }
                // alert(JSON.stringify(statisticsLog_data))
                KCApiApp.statisticsLog(statisticsLog_data)

                let data = {
                    title: this.userName +'邀请你使用灵云',
                    text: '开启灵活用工新时代',
                    logo:'http://sta.doumi.com/src/image/logo_doumi.png',
                    url:' http://a.app.qq.com/o/simple.jsp?pkgname=com.doumi.workadmin&ckey=CK1352304043982'
                }
                socialApi.shareKQTask(data)
            },
            //联系客服
            linkstr (){                
                //埋点
                let statisticsLog_data = {
                    event_id: '我-使用指南',
                    // event_label: data.event_label,(预留，现在不用传此参数)
                    event_param: {
                        ca_from: 'syzn',
                    }
                }
                // alert(JSON.stringify(statisticsLog_data))
                KCApiApp.statisticsLog(statisticsLog_data)
                location.href = `dmsaas://dmbrowser/${encodeURIComponent('https://doumi.kf5.com/hc/')}`
            },            
            //我的钱包
            mywallet (){            
                location.href = "dmsaas://MyWallet"
            },
            //获取钱包余额
            getBanlance (){
                util.ajax({
                    url:'/wallet/get',
                    type:'GET',
                    data:{
                        team_id:0,
                        project_id:0
                    },
                    timeout:10000,
                    success:(obj) => {
                        console.log(obj)
                        if(obj&&obj.errno == 0){
                            this.balance = obj.data.balance
                        }
                        
                    },
                    error: (xhr, status) => {
                        widget.toast('服务器异常')
                    },
                    noNetwork: () => {
                        widget.toast('网络连接失败，请检查网络')
                    }
                })
            }
        },
        created(){
            this.init();
            
        }
        
    }

    
</script>

<!-- CSS 样式 -->
<style src="../../node_modules/weui/dist/style/weui.css"></style>
<style src="../../static/css/common.css"></style>
<style src="../../static/css/team.css"></style>
<style>
    .weui-cell:before{
        left: 40px!important;
    }
    .weui-cell__ft{
        color: rgb(118,160,215);
        font-weight: bold;
    }
    .balance{
        font-size: 14px;
        color: #999;
        margin-right: 4px!important;
    }
</style>



